package com.ptswitch.material.pages.contract;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.AppRuntimeException;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.model.contract.ContractItem;
import com.ptswitch.material.model.security.Role;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.ContractItemService;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.service.ErrorInputCacheService;
import com.ptswitch.material.util.ContractUtil;
import com.ptswitch.material.util.security.PermissionUtil;

public class ReturnItems extends PageBase {
    @Inject
    private ContractService contractSer;
    @Inject
    private ContractItemService contractItemSer;
    @Inject
    private ErrorInputCacheService errorInputCacheSer;
    @Property
    private long contractId;
    private Contract contract;
    @Property
    private List<ContractItem> list;
    //
    @InjectPage
    private ViewContract viewContract;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(long id) {
        this.contractId = id;
        contract = contractSer.findById(id);
        checkPermitted(PermissionUtil.ContractItemReturn);
        list = errorInputCacheSer.getReturnItems(id, getVisitor());
        if (list == null) {
            list = contractItemSer.findByReturn(id);
        }
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return contractId;
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "return")
    Object update() {
        checkPermitted(PermissionUtil.ContractItemReturn);
        //just check
        for (ContractItem item : list) {
            if (item.getNumCurrentReturn() > item.getNumReceiving()) {
                errorInputCacheSer.cacheReturnItems(contractId, getVisitor(), list);
                throw AppRuntimeException.error(item.getCode() + "退货数大于已收货数");
            }
        }
        //update
        for (ContractItem item : list) {
            item.returnItem(item.getNumCurrentReturn());
            item.computePrice();//update receiving price
        }
        contractItemSer.saveAll(list);
        errorInputCacheSer.removeReturnItems(contractId, getVisitor());
        // update contract receiving ok
        Contract contract = contractSer.findById(contractId, ContractService.Init_Items);
        contract.updateForItem();
        contractSer.save(contract);
        //send email
        return viewContract.withInit(contractId);
    }

    @Override
    protected List<Role> appendRoleToVisitor(Uzer visitor) {
        return ContractUtil.appendThisRole(contract, visitor);
    }
}
